#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cstring>
#include<string>
#define endl "\n"
using namespace std;
int nxt[10005] = { 0 };
int a[1000100], b[10005];
bool s = 0;
int main()
{
	int N, an, bn;
	cin >> N;
	while (N--)
	{
		s = 0;
		cin >> an >> bn;
		for (int i = 0; i < an; i++)
		{
			scanf_s("%d", &a[i]);
		}
		for (int i = 0; i < bn; i++)
		{
			scanf_s("%d", &b[i]);
		}
		nxt[0] = -1;
		int l, r;
		l = -1, r = 0;
		while (r<bn)
		{
			if (l == -1 || b[l] == b[r])
			{
				l++;
				r++;
				nxt[r] = l;
			}
			else
				l = nxt[l];
		}
		l = 0, r= 0;
		while (r<an&&l<bn)
		{
			if (l == -1 || b[l] == a[r])
			{
				l++;
				r++;
				if (l == bn)
				{
					cout << r - l + 1 << endl;
					s = 1;
					break;
				}
			}
			else
			{
				l = nxt[l];
			}
		}
		if (!s)
			cout << "-1" << endl;
	}
	return 0;
}